import cv2
import sys

facecascPath = "src/Webcam-Face-Detect/data/haarcascade_frontalface_default.xml"
left_eyecascPath = "src/Webcam-Face-Detect/data/haarcascade_lefteye_2splits.xml"
right_eyecascPath = "src/Webcam-Face-Detect/data/haarcascade_righteye_2splits.xml"
faceCascade = cv2.CascadeClassifier(facecascPath)
left_eyeCasecade = cv2.CascadeClassifier(left_eyecascPath)
right_eyeCasecade = cv2.CascadeClassifier(right_eyecascPath)


video_capture = cv2.VideoCapture('./video/114514.mp4')

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    left_eye = left_eyeCasecade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=10,
        minSize=(15,15),
    )

    right_eye = right_eyeCasecade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=10,
        minSize=(15,15),
    )


    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),

    )

    for (x, y, w, h) in left_eye:
        cv2.rectangle(frame, (x,y), (x+w, y+h),(255, 0, 0),2)
        left_eye_image=frame[y:y+h, x:x+w]

    for (x, y, w, h) in right_eye:
        cv2.rectangle(frame, (x,y), (x+w, y+h),(255, 0, 0),2)
        right_eye_image=frame[y:y+h, x:x+w]


    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        faceimage=gray[y:y+h, x:x+w]

    # Display the resulting frame
    cv2.imshow('Video', frame)
    cv2.imshow('face', faceimage)
    cv2.imshow('left_eye', left_eye_image)
    cv2.imshow('rght_eye', right_eye_image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything is done, release the capture
# video_capture.release()
cv2.destroyAllWindows()
